package com.eightywork.life.activity;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.adapter.ChooseGroupListAdapter;
import com.eightywork.life.model.Mtag;
import com.eightywork.life.service.CacheService;
import com.eightywork.life.service.CpService;
import com.eightywork.life.service.SpaceService;
import com.eightywork.life.util.MyHttpException;
import com.umeng.analytics.MobclickAgent;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

public class GroupChooseActivity extends BaseActivity implements
		OnClickListener {
	private ListView listView;
	private ChooseGroupListAdapter adapter;
	private List<Mtag> list = new ArrayList<Mtag>();;
	private ImageButton leftButton, completeButton;
	private AsyncTask task, join;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_choose_group);
		init();
		getList();
	}
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}


	@Override
	protected void onDestroy() {
		if (task != null) {
			task.cancel(true);
		}
		if (join != null) {
			join.cancel(true);
		}
		super.onDestroy();
	}

	private void init() {
		initHeader("选圈子");
		findViewById(R.id.button_right).setVisibility(View.INVISIBLE);
		leftButton = (ImageButton) findViewById(R.id.button_left);
		completeButton = (ImageButton) findViewById(R.id.complete_button);
		leftButton.setOnClickListener(this);
		completeButton.setOnClickListener(this);
		listView = (ListView) findViewById(R.id.listView1);
		adapter = new ChooseGroupListAdapter(context, list);
		listView.setAdapter(adapter);
	}

	public void getList() {
		task = new GroupTask().execute();
	}

	class GroupTask extends AsyncTask<Void, Void, String> {
		private List<Mtag> aList;

		@Override
		protected void onPreExecute() {
			showProgressDialog("请稍后...");
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			try {
				aList = SpaceService
						.mtag(CacheService.getSpace().getXiaoquid());

			} catch (MyHttpException e) {
				return e.getErrorMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			hideProgressDialog();
			if (result == null) {
				if (aList != null) {
					list.clear();
					list.addAll(aList);
				}
				adapter.notifyDataSetChanged();
			} else {
				showMessage(result);
			}
			super.onPostExecute(result);
		}
	}

	class JoinTask extends AsyncTask<Void, Void, String> {
		@Override
		protected void onPreExecute() {
			showProgressDialog("请稍后...");
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... arg0) {
			try {
				String ids = "";
				List<String> sList = adapter.getSelected();
				for (int i = 0; i < sList.size(); i++) {
					ids += sList.get(i);
					if (i < sList.size() - 1)
						ids += "|";
				}
				CpService.mtagjoin(ids);
			} catch (MyHttpException e) {
				return e.getErrorMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			hideProgressDialog();
			if (result != null) {
				showMessage(result);
			} else {
				Intent intent = new Intent(context, IndexActivity.class);
				intent.putExtra("beginToload", true);
				startActivity(intent);
				finish();
				LoginMapActivity.activity.finish();
			}
			super.onPostExecute(result);
		}
	}

	@Override
	public void onClick(View v) {
		if (v == leftButton) {
			Intent intent = new Intent(context, FriendChooseActvity.class);
			startActivity(intent);
			finish();
		} else if (v == completeButton) {
			join = new JoinTask().execute();
		}
	}
}
